var bExtraDebug = true;

function connectToBackground(fSuccessCallback) {
  backgroundRequest("loadManifest", null,
    function loadManifestSuccess(oManifest) {
      goManifest = oManifest;
      if (bExtraDebug) debugLog('Manifest: ' + repr(oManifest));
      backgroundRequest("loadOptionsStructure", null,
        function loadOptionsStructureSuccess(oOptionsStructure) {
          goOptionsStructure = oOptionsStructure;
          if (bExtraDebug) debugLog('Options structure: ' + repr(oOptionsStructure));
          loadOptions(
            fSuccessCallback, 
            function loadOptionsFailure(sErrorMessage) {
              debugLog(sErrorMessage);
            }
          );
        },
        function loadOptionsFailure(sErrorMessage) {
          debugLog(sErrorMessage);
        }
      );
    }, 
    function loadManifestFailure(sErrorMessage) {
      debugLog(sErrorMessage);
    }
  );
}
function loadOptions(fSuccessCallback, fFailureCallback) {
  backgroundRequest("loadOptions", null,
    function loadOptionsSuccess(oOptions) {
      if (bExtraDebug) debugLog('Options: ' + repr(oOptions));
      goOptions = oOptions;
      if (fSuccessCallback) fSuccessCallback();
    },
    fFailureCallback
  );
}
function saveOptions(fSuccessCallback, fFailureCallback) {
  backgroundRequest('saveOptions', goOptions, fSuccessCallback, fFailureCallback);
}

